package com.pubinfo.passbook.api.controller.bill;

import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.entity.TFileInfo;
import com.pubinfo.passbook.common.mapper.TFileInfoMapper;
import com.pubinfo.passbook.common.model.dto.file.ArtificialFileParam;
import com.pubinfo.passbook.common.model.dto.file.UploadFileParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.api.service.ForwardFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "file")
@Api(tags = "文件信息")
@Slf4j
public class TFileController {
  @Autowired
  ForwardFileService forwardFileService;
  @Autowired
  TFileInfoMapper tFileInfoMapper;

  
  @PostMapping("/uploadFile")
  @ApiOperation(value = "通过id上传发票或票据的文件,type为1为票据2为发票")
  public String uploadInvoiceById(UploadFileParam uploadFileParam) throws UnirestException {
    return forwardFileService.uploadInvoiceById(uploadFileParam);
  }


  @PostMapping("/uploadArtificialFile")
  @ApiOperation(value = "上传人工报账相关文件")
  public String uploadArtificialFile(ArtificialFileParam artificialFileParam) throws UnirestException {
    return forwardFileService.uploadArtificialFile(artificialFileParam);
  }

  
//  @PostMapping("/selectFile")
//  @ApiOperation(value = "查询文件")
//  public JsonResult<Page<TFileInfo>> selectAllFileInfo(@RequestBody FileListParam fileListParam) throws IOException {
//    return JsonResult.success(new Page(tFileInfoService.selectFileInfoByFileListParam(fileListParam)));
//  }
//
  @GetMapping("list/{artificialId}")
  @ApiOperation(value = "根据人工上传id查询所有的文件")
  public JsonResult<List<TFileInfo>> listByInvoiceId(@PathVariable("artificialId") String artificialId) {
    return JsonResult.success(tFileInfoMapper.selectByArtificialId(artificialId));
  }

  @PostMapping("/saveTFileEnclosure")
  @ApiOperation(value = "上传票据相关文件")
  public String saveTFileEnclosure(MultipartFile file) throws UnirestException {
    return forwardFileService.saveTFileEnclosure(file);
  }

  @DeleteMapping("/removeFile")
  @ApiOperation(value = "软删除文件")
  public JsonResult<Boolean> removeFile(@RequestBody List<String> id) {
    if(!CollectionUtils.isEmpty(id)) return JsonResult.success(tFileInfoMapper.softDeleteFile(id));
    return JsonResult.error("数组为空");
  }
  
  @GetMapping("/acctDownload/{id}")
  @ApiOperation(value = "用户根据文件id下载文件")
  public void acctDownload(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) throws UnirestException {
    forwardFileService.acctDownload(request,response,id);
  }

  @GetMapping("/acctDownloadZip")
  @ApiOperation(value = "用户根据文件id下载多个文件的压缩包")
  public void acctDownloadZip(HttpServletRequest request, HttpServletResponse response, @RequestBody List<String> id) throws IOException, UnirestException {
    forwardFileService.acctDownloadZip(request, response, id);
  }
}
